package com.ruoyi.web.controller.system;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.ClimbingPriceComparisonData;
import com.ruoyi.system.domain.ShopClassProductDetail;
import com.ruoyi.system.service.ClimbingPriceComparisonDataService;
import com.ruoyi.system.service.ShopClassProductDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/***
 * 说明  导入Excel  数据源数据
 * @author:ZZ
 * @date: 2024-12-02  22:25
 **/

@RestController
@RequestMapping("/system/erport")
@Api(tags = "解析上传的Excel文件内容")
public class ExportExcelDataResultController  extends BaseController {

    @Autowired
    private ClimbingPriceComparisonDataService service;

    @PostMapping("/uploadExcelFile")
    @ApiOperation("解析上传的Excel文件内容...")
    @Transactional
   public Object uploadExcelFile(@RequestParam("file") MultipartFile file){
        try {
            List<ClimbingPriceComparisonData> dataList = readExcel(file);
if (null != dataList && dataList.size() >0) {
    boolean b = service.saveBatch(dataList);// 保存
    System.out.println("保存-=---   "+b);
    return success(dataList);
}else {
    return success("没有可解析数据");
}
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        }
    }


    private List<ClimbingPriceComparisonData> readExcel(MultipartFile file) {
        ExcelReader reader = null;
        try {
            reader = ExcelUtil.getReader(file.getInputStream());
            reader.addHeaderAlias("id（不填，导出用）", "id");
            reader.addHeaderAlias("category_id必填", "categoryId");
            reader.addHeaderAlias("title", "title");
            reader.addHeaderAlias("description", "description");
            reader.addHeaderAlias("level", "level");
            reader.addHeaderAlias("pid", "pid");
            reader.addHeaderAlias("icon", "icon");
            reader.addHeaderAlias("cover_img", "coverImg");
            reader.addHeaderAlias("url必填", "url");
            reader.addHeaderAlias("product_num", "productNum");
            reader.addHeaderAlias("status", "status");
            reader.addHeaderAlias("对应产品数量", "quantityProduct");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 根据你的实体类属性继续添加别名映射
        return reader.readAll(ClimbingPriceComparisonData.class);
    }



    @Autowired
    private ShopClassProductDetailService productDetailService;

    @PostMapping("/uploadClassExcelFile")
    @ApiOperation("解析上传的Excel文件内容...分类Excel解析内容  ")
//    @Transactional
    public Object uploadClassExcelFile(@RequestParam("file") MultipartFile file){
        try {
            List<ShopClassProductDetail> dataList = readClassExcel(file);
            if (null != dataList && dataList.size() >0) {
                boolean b = productDetailService.saveOrUpdateBatch(dataList);// 保存
                 return success(dataList);
            }else {
                return success("没有可解析数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        }
    }

    private List<ShopClassProductDetail> readClassExcel(MultipartFile file) {
        // 标题	标题链接	图片	价格	px8px
        ExcelReader reader = null;
        try {
            reader = ExcelUtil.getReader(file.getInputStream());
            reader.addHeaderAlias("标题", "title");
            reader.addHeaderAlias("标题链接", "url");
            reader.addHeaderAlias("图片", "img");
            reader.addHeaderAlias("价格", "price");
            reader.addHeaderAlias("px8px", "comparativeNumberStores");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 根据你的实体类属性继续添加别名映射
        return reader.readAll(ShopClassProductDetail.class);
    }



}
